package de.dnb.gnd.utils.formatter;

import java.awt.print.PrinterException;
import java.util.Collection;

import javax.swing.JEditorPane;
import javax.swing.JFrame;

import de.dnb.basics.utils.HTMLUtils;
import de.dnb.gnd.parser.Record;
import de.dnb.gnd.parser.Subfield;
import de.dnb.gnd.parser.line.Line;
import de.dnb.gnd.parser.tag.GNDTagDB;
import de.dnb.gnd.utils.BibRecUtils;
import de.dnb.gnd.utils.GNDUtils;
import de.dnb.gnd.utils.RecordUtils;
import de.dnb.gnd.utils.SubfieldUtils;

public class HTMLFormatter2 extends Pica3Formatter {

	private boolean boldLine;

	private int fontsize = 10;

	private int border = 0;

	private int spacing = 0;

	private int padding = 2;

	public void setBorder(int border) {
		this.border = border;
	}

	public void setSpacing(int spacing) {
		this.spacing = spacing;
	}

	public void setPadding(int padding) {
		this.padding = padding;
	}

	public void setFontsize(int fontsize) {
		this.fontsize = fontsize;
	}

	public static void main(String[] args) {
		Record record = BibRecUtils.readFromClip();
		HTMLFormatter2 formatter = new HTMLFormatter2();
		formatter.setFontsize(16);
		formatter.setBorder(0);
		formatter.setSpacing(-3);
		String txt = formatter.format(record);
//		System.out.println(txt);
		HTMLUtils.show(txt);
		//		try {
		//			HTMLUtils.printWithoutDialog(txt);
		//		} catch (PrinterException e) {
		//			// TODO Auto-generated catch block
		//			e.printStackTrace();
		//		}
	}

	@Override
	public String format(Record record) {
		return HTMLUtils.span(HTMLUtils.table(super.format(record), border,
			spacing, padding));
	}

	@Override
	protected String formatContent(String content) {
		return content.replace("@", "&#64;");
	}

	@Override
	public String format(Line line) {
		actualTag = line.getTag();
		boldLine = false;
		String pica3 = actualTag.pica3;
		if (pica3.startsWith("1") && pica3.length() == 3)
			boldLine = true;
		else {
			if (SubfieldUtils.containsIndicator(line, '4')) {
				Subfield subfield4 = SubfieldUtils.getFirstSubfield(line, '4');
				String subCont = subfield4.getContent();
				if (subCont.equals("aut1") || subCont.equals("kom1")
					|| subCont.equals("kue1"))
					boldLine = true;
			}
		}
		String s = HTMLUtils.tableCell(formatTag(), fontsize);
		Collection<Subfield> subfields = line.getSubfields(format);
		s += HTMLUtils.tableCell(format(subfields), fontsize);
		return HTMLUtils.tableLine(s);
	}

	@Override
	public String formatTag() {
		if (boldLine)
			return HTMLUtils.bold(super.formatTag());
		else
			return super.formatTag();
	}

	@Override
	public String format(Collection<Subfield> subfields) {
		String subsString = super.format(subfields);
		/*
		 * Ziemlich primitiv, ist aber das Verhalten, das die Win-IBW
		 * auch liefert: es werden einfach $ und das darauf folgende
		 * Zeichen farblich hervorgehoben.
		 */
		subsString =
			subsString.replaceAll("(\\$.)",
				"<font color=\"#CC3300\"><b>$1</b></font>");

		if (boldLine)
			return HTMLUtils.bold(subsString);
		else
			return subsString;
	}

	@Override
	protected String format(Subfield subfield) {
		String s = super.format(subfield);
		char c = actualIndicator.indicatorChar;
		if (c == '8') {
			s = HTMLUtils.italic(s);
		} else if (c == '9') {
			String id = subfield.getContent();
			s = HTMLUtils.makeHtmlRef(id, "!" + id + "!");
		}
		return s;
	}

}
